//
//  UIViewController+BDAddBusinessHelper.m
//  HSY
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "UIViewController+BDAddBusinessHelper.h"
#import "BDFillBusinessInfoStep2ViewController.h"
#import "BDFillBusinessInfoStep3ViewController.h"
#import "BDFillBusinessInfoStep4ViewController.h"
#import "BDFillBusinessInfoStep5ViewController.h"
#import "BDFillBusinessInfoSuccessViewController.h"

#import "BDAddBusinessInfoViewController.h"

@implementation UIViewController (BDAddBusinessHelper)

- (NSDictionary<NSString *, NSString *> *)stepsViewControllerMap{
    return @{
             @"-1": @"BDAddBusinessInfoViewController",
             @"0" : @"BDFillBusinessInfoStep2ViewController",
             @"1" : @"BDFillBusinessInfoStep3ViewController",
             @"2" : @"BDFillBusinessInfoStep4ViewController",
             @"3" : @"BDFillBusinessInfoStep5ViewController",

             };
}

- (void)nextStepWihBusinessModel:(BDBusinessModel *)model userShopModel:(BDUserShopModel *)userShopModel readOnly:(BOOL) readOnly{
    UIViewController *vc;
    NSInteger step = [model.auStep integerValue];
    if (!model) {
        step = -1;
    }
    if (model.status == 1) { //审核通过
        readOnly = YES;
    }
    
    if (readOnly || model.status != 1) {
        HSYStepBaseViewController *stepVC = [self stepViewController:step];
        stepVC.model = model;
        stepVC.userShopModel = userShopModel;
        stepVC.readOnly = readOnly;
        
        vc = stepVC;

    }
    
    if (vc) {
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [self popToStartViewController];
    }
}

- (HSYStepBaseViewController *)stepViewController:(NSInteger)step{
    NSString *stepViewControllerClassName = [[self stepsViewControllerMap] objectForKey:[NSString stringWithFormat:@"%ld", (long)step]];
    
    if (!stepViewControllerClassName) return nil;
    
    HSYStepBaseViewController *vc = [[NSClassFromString(stepViewControllerClassName) alloc] init];
    return vc;
}

- (BOOL)viewControllerIsStepViewController:(UIViewController *)viewController{
    BOOL b = NO;
    if (viewController) {
        NSString * stepViewControllerClassName = NSStringFromClass([viewController class]);
        for (NSString *className in [self stepsViewControllerMap].allValues) {
            if ([className isEqualToString:stepViewControllerClassName]) {
                b = YES;
                break;
            }
        }
    }
    return b;
}

- (void)popToStartViewController{
    if (self.navigationController && self.navigationController.viewControllers.count > 1) {
        UIViewController *toVC = nil;
        for (UIViewController *vc in self.navigationController.viewControllers) {
            if ([self viewControllerIsStepViewController:vc]) {
                break;
            }else{
                toVC = vc;
            }
        }
        if (toVC)
            [self.navigationController popToViewController:toVC animated:YES];
        else
            [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
